/**
 * 
 */
package hichecker.ui.custom_views.conflict_handling;

import hichecker.ui.custom_views.series.SeriesViewTableModel;
import hichecker.university.PTLSeries;
import hichecker.university.Series;
import hichecker.university.Student;
import hichecker.university.Task;

import java.util.List;

/**
 *
 */
public class ConflictOpenedTableModel extends SeriesViewTableModel {

	protected List<Student> allStudents;
	
	public ConflictOpenedTableModel(List<Student> students, Series series, List<Student> allStudents) {
		super(students, series);
		this.allStudents = allStudents;
	}
	
	@Override
	public Object getValueAt(int row, int column) {
		// Out of range
		if (row >= getRowCount() || column >= getColumnCount()) return null;
		
		// Student doesn't exist in this table, so return null
		Student selStudent = allStudents.get(row);
		if (!(students.contains(selStudent)))
			return null;

		// Set the rowindex to the right value and get the student
		row = students.indexOf(selStudent);
		selStudent = students.get(row);
		
		// Default fixed column
		switch(column) {
			case 0: return selStudent.getLastName();
			case 1: return selStudent.getFirstName();
			case 2: return selStudent.getMatrikel();
			case 3: return selStudent.getStudyCourse();
		}

		// Look in dynamic columns
		column -= FIXED_COLUMNS;
		if (column >= dynamicColumns.size()) return null;
		
		switch (dynamicColumns.get(column)) {
			case TASK:
				float gainedPoints = series.getGainedPoints(selStudent, column);
				return gainedPoints < 0 ? null : gainedPoints;
			case GRADE: return ((PTLSeries) series).getGrade(selStudent);
			default: return null;
		}
	}
	
	@Override
	public int getRowCount() {
		return allStudents == null ? 0 : allStudents.size();
	}

	@Override
	public int getColumnCount() {
		return super.getColumnCount() - 1;
	}
	
	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}
	
	@Override
	public Object[] getHeaderValues() {
		Object[] tmpHeaderValues = super.getHeaderValues();
		// Adjust the last header column so it's the task not the sum
		Task task = series.getTaskAt(series.getNumTasks() - 1);
		tmpHeaderValues[tmpHeaderValues.length - 1] = 
			"<html>" + task.getName() + (task.isOptional() ? "*" : "") + "<br>" + task.getMaxPoints();
		return tmpHeaderValues;
	}
	
}
